package com.hsxshop.controller;

import com.hsxshop.model.Pagination;
import com.hsxshop.model.Supply;
import com.hsxshop.service.SupplyService;
import com.hsxshop.util.*;
import lombok.extern.java.Log;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.IOException;
import java.sql.Array;
import java.util.*;
import java.util.List;

/**
 * Created by wangchongya on 17/6/27.
 */
@RestController
@RequestMapping(value = "/supply/")
@Log
public class SupplyController {

    @Resource(name = "supplyService")
    private SupplyService supplyService;

    @RequestMapping(value = "list", method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String index(@RequestBody Map requestMap  ){
        Pagination pagination=PaginationTool.getPagination(requestMap);
//        List<Supply> list=supplyService.findByHQL(SqlTool.returnSql("*", "Supply sp", " where sp.sortTime<?0", "order by id desc"),DateTool.getStringDateShort());
        List<Supply> list=supplyService.findByHQLWithPagination(pagination, SqlTool.returnSql("new Supply(id,shipCode,phone,title,remark,sortTime,statusId)", "Supply", " where sortTime<?0", "order by id desc"), DateTool.getStringDateShort());
        List supplys=new ArrayList();
        for (Supply sp:list) {
            //查找供应详情
            Map map=new HashMap();
            map.put("id",sp.getId());
            map.put("shipCode",sp.getShipCode());
            map.put("phone",sp.getPhone());
            map.put("title",sp.getTitle());
            map.put("remark",sp.getRemark());
            map.put("sortTime",sp.getSortTime());
            map.put("statusId",sp.getStatusId());
            supplys.add(map);
        }
        //获取当前时间
        return JsonTool.returnListJsonSuccess(supplys,pagination);
    }

    @RequestMapping(value = "detail", method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String detail(@RequestBody Map requestMap){

        Integer id=(Integer)requestMap.get("id");

        Supply supply=supplyService.getEntity(id);

        return JsonTool.returnObjectJsonSuccess(supply);
    }

    @RequestMapping(value = "edit", method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,consumes =MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String edit(@RequestBody Map requestMap){
        Supply supply= new Supply();
        supply.setStorageMode(ParamConvertTool.getParamInt(requestMap,"storageMode"));
        supply.setId(ParamConvertTool.getParamInt(requestMap,"id"));
        String sql="update Supply set storageMode=? where id=?";
        if(supplyService.execHQL(sql,supply.getStorageMode(),supply.getId())==1){
            return JsonTool.returnObjectJsonSuccess(null);
        }else{
            return JsonTool.returnObjectJsonError(null);
        }

    }

}
